package com.ruoyi.coal.modules.coalpile.controller;

import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.R;
import com.ruoyi.coal.modules.coalpile.domain.TimingEntity;
import com.ruoyi.coal.modules.coalpile.service.TimingService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "timing", tags = {"定时信息"})
@RestController
@RequestMapping({"coalpile/timing"})
@DataSource(value = DataSourceType.SLAVE)
public class TimingController {
    @Autowired
    private TimingService timingService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "limit", value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "order", value = "排序方式，可选值(asc,desc)", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "key", value = "关键字", paramType = "query", dataType = "String")})
    @GetMapping({"/list"})
    @RequiresPermissions({"coalpile:timing:list"})
    public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = this.timingService.queryPage(params);
        return R.ok().put("page", page);
    }

    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping({"/info/{id}"})
    @RequiresPermissions({"coalpile:timing:info"})
    public R info(@PathVariable("id") Integer id) {
        TimingEntity timing = (TimingEntity) this.timingService.getById(id);
        return R.ok().put("timing", timing);
    }

    @ApiOperation(value = "新增定时信息", notes = "新增定时信息")
    @PostMapping({"/save"})
    @RequiresPermissions({"coalpile:timing:save"})
    public R save(@RequestBody TimingEntity timing) {
        if (this.timingService.saveJob(timing).booleanValue())
            return R.ok();
        return R.error("请与该煤场当前存在的雷达任务间隔10分钟以上");
    }

    @ApiOperation(value = "根据id修改定时信息", notes = "根据id修改定时信息")
    @PostMapping({"/update"})
    @RequiresPermissions({"coalpile:timing:update"})
    public R update(@RequestBody TimingEntity timing) {
        if (this.timingService.updateJobById(timing).booleanValue())
            return R.ok();
        return R.error("更新失败，请与该煤场当前存在的雷达任务间隔10分钟以上");
    }

    @ApiOperation(value = "通过id删除定时信息", notes = "通过id删除定时信息")
    @PostMapping({"/delete"})
    @RequiresPermissions({"coalpile:timing:delete"})
    public R delete(@RequestBody Integer[] ids) {
        this.timingService.removeJob(ids);
        return R.ok();
    }
}
